home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12799 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Structure names are pointers? Writing to them?
  5. Date: 21 Mar 1996 16:31:07 GMT
  6. Organization: Borland International
  7. Message-ID: <4is08b$in8@druid.borland.com>
  8. References: <4idmdo$2b2@news.voicenet.com>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4idmdo$2b2@news.voicenet.com>, deaton@cygnus.rsabbs.com says...
  15. >
  16. >
  17. >   I'm using several random access data files in my program(Written in 
  18. TurboC/C++), and all except for one are ARRAYS of STRUCTURES.
  19. >With these files, I have no syntax problems reading and writing to them. 
  20. >For instance:
  21. >
  22. >FILE * fptr;                         
  23. >blahS blah[15];
  24. >
  25. >...code to populate structure...
  26. >
  27. >fptr=fopen("blah.dat","w+");
  28. >fwrite(blah,sizeof(blah),1,fptr);
  29. >fclose(fptr);
  30. >
  31. >This bit of code works fine, but when I try to write only one structure:
  32. >
  33. >FILE * fptr;                         
  34. >blahS blah;
  35. >
  36. >...code to populate structure...
  37. >
  38. >fptr=fopen("blah.dat","w+");
  39. >fwrite(blah,sizeof(blah),1,fptr);
  40. >fclose(fptr);
  41. >my compiler spits out an error. As far as I understood, structure names acted 
  42. the
  43. >same way as array names: they are pointers. So why isn't this working? I 
  44. really don't
  45. >want to declare a structure array of 1 element. It would work, but that would 
  46. be pretty lame.
  47.  
  48. Structure names do not act the same way as array names, and array names are not 
  49. pointers. The name of an array decays into a pointer to its first element in 
  50. most situations, which is why the original code worked. When dealing with a 
  51. single object, however, you must tell the compiler to use its address:
  52.  
  53. fwrite( &blah,sizeof(blah),1,fptr);
  54.  
  55.